// i.r(t), i.d(t, { default: () => AppContainerResizer })

import Module from "./Module"
import s from "../packages/85917"
import browserUtil from "../utils/browser.util"
import { TextBoxFocusMessage } from "../message/text.message"
import { ToolToggledMessage } from "../message/tool.message"

import { AppContainerResizeError } from "../exception/app.exception"
export default class AppContainerResizer extends Module {
  touchDevice: boolean
  timeout: number
  keyboardFocused: boolean
  handleResize: (e: any) => void
  handleToolChange: (e: any) => void
  handleTextBoxFocus: (e: any) => void
  appContainer: any
  constructor() {
    super(...arguments)
    this.name = "container-resizer"
    this.touchDevice = browserUtil.canTouch()
    this.timeout = 0
    this.keyboardFocused = !1
    this.handleResize = e => {
      this.keyboardFocused || (document.body.style.height = e.h + "px")
    }
    this.handleToolChange = e => {
      !e.open && this.keyboardFocused && this.setKeyboardFocus(!1)
    }
    this.handleTextBoxFocus = e => {
      this.setKeyboardFocus(e.focused)
    }
  }
  async init(e, t) {
    if (!e.container) throw new AppContainerResizeError()
    this.appContainer = e.container
    this.touchDevice &&
      (this.bindings.push(
        s.createSubscription(s.resizeEnum.RESIZE, this.handleResize),
        t.subscribe(TextBoxFocusMessage, this.handleTextBoxFocus),
        t.subscribe(ToolToggledMessage, this.handleToolChange)
      ),
      this.handleResize(s.initSize()))
    for (const e of this.bindings) e.renew()
  }
  setKeyboardFocus(e) {
    this.keyboardFocused = e
    window.clearTimeout(this.timeout)
    this.keyboardFocused
      ? ((document.body.style.height = ""),
        (this.timeout = window.setTimeout(() => {
          document.documentElement.classList.add("keyboard-layout")
        }, 300)))
      : (document.documentElement.classList.remove("keyboard-layout"), (this.appContainer.scrollTop = 0))
  }
}
